package com.gao.security.ferrmarker.core.context.authentication.domain;


import com.gao.security.ferrmarker.core.base.ApiEntity;
import com.gao.security.ferrmarker.core.base.Credentials;
import com.gao.security.ferrmarker.core.base.UserInfo;
import com.gao.security.ferrmarker.core.util.MD5Util;
import lombok.Data;

/**
 * <p>
 * 账户域
 * </p>
 *
 * @author GaoShuangWen
 * @since 2019-07-17 16:09
 */
@Data
public class AccountInfo implements ApiEntity {

    private AccountId accountId;

    private Credentials password;

    private UserInfo userInfo;

    private AccountInfo(AccountId accountId, Password password) {
        this.accountId = accountId;
        this.password = password;
    }

    /**
     * 构建账户信息的唯一入口
     * @param userId
     * @param password
     * @return
     */
    public static AccountInfo getInstance(String userId, String password) {
        return new AccountInfo(AccountId.newInstance(userId), Password.newInstance(password));
    }

    @Override
    public Object getIdentify() {
        return getAccountId().getName();
    }

    /**
     * 登录验证
     * @param password
     * @return
     */
    public boolean login(String password){
        String s = MD5Util.stringMD5(password);
        assert s != null;
        String credentials = (String) this.password.getCredentials();
        return s.equals(credentials);
    }


}
